/*
 * @Author: your name
 * @Date: 2020-08-26 15:22:31
 * @LastEditTime: 2020-08-26 15:29:26
 * @LastEditors: Please set LastEditors
 * @Description: 装饰器实际使用的小例子
 * @FilePath: /decorator/src/demo.ts
 */
const userInfo: any = undefined;

function catchError(msg: string) {
  return function(target:any, key: string, descriptor: PropertyDescriptor) {
    const fn = descriptor.value;
    descriptor.value = function () {
      try {
        fn();
      }catch (e) {
        console.log(msg);
      }
    }
  }
}

class Demo {
  @catchError('userInfo.name 不存在')
  getName() {
    return userInfo.name;
  }
  @catchError('userInfo.age 不存在')
  getAge() {
    return userInfo.age;
  }
  @catchError('userInfo.gender 不存在')
  getGender() {
    return userInfo.gender;
  }
}

const demo = new Demo();
demo.getName();
demo.getAge();
